﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;
using Roles = Medianamik.Core.Security.Roles;

namespace Medianamik.UI.Web.ModalWindows.ContentManagement
{
    public partial class ListTypeSelectorWindow : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var types = NodeTypeManager.GetAllTypes(false)
                    .Where(t => !t.IsSystem 
                        && t.Listable 
                        && Roles.UserHasRight(t, Core.Security.SecurityLevels.Create));
                ddlNodeTypes.DataSource = types;
                ddlNodeTypes.DataValueField = "ID";
                ddlNodeTypes.DataTextField = "LocalizedName";
                ddlNodeTypes.DataBind();
                ddlNodeTypes.Items.Insert(0,
                     new ListItem(Translator.Translate("Medianamik_ContentManagement", "SelectType"), Guid.Empty.ToString()));

                var id = Request.QueryString[DefaultParamNames.TypeID];
                if (id != null)
                {
                    Guid typeId;
                    if (StringHelper.IsGuid(id, out typeId) && types.Any(t => t.ID == typeId))
                    {
                        ddlNodeTypes.SelectedValue = typeId.ToString();
                    }
                }
            }
        }

        private void ShowError(string error)
        {
            phrMain.Visible = false;
            phrError.Visible = true;
            litError.Text = error;
        }
    }
}
